package cate.game.pvp.teampvp;

import cate.common.table.d.GDVideoHall;
import cate.game.play.part.PlaySide;
import cate.game.play.proce.play.PlayPO;
import cate.game.play.proce.play.PlayStory;
import com.fasterxml.jackson.annotation.JsonIgnore;
import cp.solution.util.collection.MapKit;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.apache.commons.collections4.MapUtils;
import org.springframework.data.annotation.Id;

import java.time.LocalDateTime;
import java.util.Date;

@NoteClass(value = "一场录像的快照")
public class TeamPvpReplayProfile {
    @NoteField(value = "录像唯一id")
    @Id
    public long replayUid;

    @NoteField(value = "剧本故事")
    public PlayStory story;

    @NoteField(value = "A方选手阵容")
    public PlaySide a;

    @NoteField(value = "B方选手阵容")
    public PlaySide b;

    @NoteField(value = "胜利方")
    public byte winner;

    @NoteField(value = "录像生成日期")
    public Date createDate;

    @NoteField(value = "剧本的播放时间")
    public long playTime;

    @NoteField(value = "回合数")
    public int round;

    @NoteField(value = "最大回合数")
    public int roundMax;

    @NoteField(value = "剧本过期时间", detail = "各个功能单独设置")
    public LocalDateTime expiredDate;

    @JsonIgnore
    public boolean isExpired(int group) {
        int day = GDVideoHall.getRecordKeepDay(group);
        if(day < 0){
            return false;
        }
        // 理论上可以根据分组id来确认过期时间
        long past = System.currentTimeMillis() - createDate.getTime();
        return past / 86400000 >= day;
    }

    public static TeamPvpReplayProfile buildProfile(PlayPO play) {
        TeamPvpReplayProfile profile = new TeamPvpReplayProfile();
        profile.story = play.story.copy();
        if (MapUtils.isNotEmpty(play.story.params)) {
            profile.story.params = MapKit.clone(play.story.params);
        }
        profile.a = play.a.copy();
        profile.b = play.b.copy();
        // 根据血量继承删除已经阵亡的英雄
        profile.winner = play.winner;
        profile.createDate = new Date(play.startTime);
        profile.playTime = play.playTime();
        profile.round = play.rounds.size();
        profile.roundMax = play.roundMax;
        profile.expiredDate = play.expiredDate;
        return profile;
    }

    public TeamPvpReplayProfile copy() {
        TeamPvpReplayProfile profile = new TeamPvpReplayProfile();
        profile.replayUid = this.replayUid;
        profile.story = this.story.copy();
        profile.a = this.a.copy();
        profile.b = this.b.copy();
        profile.winner = this.winner;
        profile.createDate = this.createDate;
        profile.playTime = this.playTime;
        profile.round = this.round;
        profile.roundMax = this.roundMax;
        profile.expiredDate = this.expiredDate;
        return profile;
    }
}
